if( !jsx3.lang.Class.forName("tibco.admin.monitormanage.uddiPub.UddiPub") ){

tibco.admin.Main.loadResource("tibco/admin/monitormanage/uddiPub/jss/uddiPubJSS.xml", "tibco.admin", "uddiPubJSS_47", "jss");
tibco.admin.Main.loadResource("tibco/admin/monitormanage/uddiPub/jss/uddiPubTextJSS.xml", "tibco.admin", "uddiPubTextJSS_47", "ljss");
tibco.admin.Main.loadResource("tibco/admin/monitormanage/uddiPub/jss/xslt.xml", "tibco.admin", "xslt_47", "jss");

/**
*
*/
jsx3.lang.Class.defineClass(
		"tibco.admin.monitormanage.uddiPub.UddiPub",
		jsx3.lang.Object,
		[tibco.admin.monitormanage.uicomponents.page.serviceView.ServiceTabInterface],
		function (plugin) {

			/* mcStatus enumeration values
			PublishIncomplete => Publish, Unpublish, 
			UnpublishedIncomplete => Publish, Unpublish
			Published => Unpublish
			Unpublished => Publish
			*/
			plugin.instance = null

			plugin.getInstance = function(args) {
				if(!this.instance) {
					this.instance = new tibco.admin.monitormanage.uddiPub.UddiPub();
				}
				return this.instance;
			}

			plugin.prototype.getNamespace = function() {
				return "tibco.admin";
			}

			plugin.prototype.getTabText = function() {
				return tibco.admin.Main.getServer().getDynamicProperty("@@lblSectionTitle_37");
			}

			plugin.prototype.updateComponent = function(serviceId,sType,serviceName) {
				plugin.EntityID = serviceId;
				plugin.EntityName = serviceName;
			}

			plugin.prototype.loadComponent = function(parent) {
				tibco.admin.Main.loadUIResourceAsync(parent, this.getNamespace(),
						"tibco/admin/monitormanage/uddiPub/ui/uddiPub.xml", this, this.componentLoaded);
			}

			plugin.prototype.componentLoaded = function(objEvent) {
				if(objEvent.status == "SUCCESS") {
					this.block = objEvent.obj;
					var me = this;
					this.block.handler = me;
					this.callgetUDDIServerInfo();
					this.callgetMetadata();
					this.callgetUDDIIDforService();
				}
				else{
					jsx3.log(objEvent.message);
					if(objEvent.exception){
					  jsx3.log(objEvent.exception.printStackTrace());
					}
				}
			}

			//get the data for the matrix of uddi servers
			plugin.prototype.callgetUDDIServerInfo = function() {
				var objService = tibco.admin.Main.loadService("tibco.admin",
						"tibco/admin/monitormanage/uddiPub/rules/getUDDIServerInfo.xml",
						"getUDDIServerInfo",
						"getUDDIServerInfo");

				if(tibco.admin.Main.getServer().getEnv("MODE")) {
					objService.setEndpointURL("/amxadministrator/services/monitorandmanage");
				}

				objService.EntityID = plugin.EntityID;
				objService.EntityName = plugin.EntityName;
				objService.subscribe(jsx3.net.Service.ON_SUCCESS, this, this.ongetUDDIServerInfoSuccess);
				objService.doCall();
			};


			plugin.prototype.ongetUDDIServerInfoSuccess = function(objEvent){
				var uddiServerList = tibco.admin.Main.getServer().getCache().getDocument("uddiServerList");
				var serverRecordArray = uddiServerList.selectNodes("/data/record");

				//if uddi server records existed, then load instance of service.modified by peter
				if(serverRecordArray.size() > 0){
					this.callgetInstancesOfService();
				}

				for(var i=0;i<serverRecordArray.size();i++){
					var serverRecord = serverRecordArray.get(i);
					var mcStatus = serverRecord.getAttribute("mcStatus");
					if(mcStatus == "Unpublished"){
						serverRecord.setAttribute("mcBusiness",  tibco.admin.Main.getServer().getDynamicProperty("@@txtSelect_37") );
					}
				}

				this.block.getDescendantOfName("mtxUddiServers").repaint();
			};


			plugin.prototype.btnAddMetadata_onclick = function(){
				this.uddiPubDlg = tibco.admin.Main.getServer().getBodyBlock().load("tibco/admin/monitormanage/uddiPub/ui/uddiPubDlg.xml");
				var me = this;
				this.uddiPubDlg.handler = me;
				this.callgetMetadataForService();
			}

			plugin.prototype.btnSave_DLG_onclick = function(){
				//Remove the old containers
				var objCDFLogCfg = this.block.getDescendantOfName("mtxLogCfg_36").getXML();
				var firstChild = objCDFLogCfg.getFirstChild();
				objCDFLogCfg.removeChildren();
				objCDFLogCfg.appendChild(firstChild);

				//Add containers to log list
				var objCDFDocument = this.logCfgDlg.getDescendantOfName("mtxService_36").getXML();
				var list = objCDFDocument.selectNodes("//record[@checked=1]");
				var iter = list.iterator();
				while (iter.hasNext()) {
					try {
						var obj = iter.next();
						objCDFLogCfg.appendChild(obj);
					}
					catch(ex) {
						tibco.admin.Main.logException(ex);
					}
				}
				this.logCfgDlg.doClose();
				this.block.getDescendantOfName("mtxLogCfg_36").repaintData();
				this.btnEdit_onclick();
			}

			/**
			 * set the dropdown list data dynamicly here
			 */
			plugin.prototype.mtx_isEditable = function(id) {
				var objCDFDocument = this.block.getDescendantOfName("mtxUddiServers").getXML();
				var serverRecord = objCDFDocument.selectSingleNode("//record[@jsxid='" + id + "']");

				// if clicked uddi server already published, then ignore
				if(serverRecord.getAttribute("mcStatus") != "Unpublished") {
					return false;
				}

				// if uddi server is unreachable, then ingore
				if (serverRecord.getAttribute("serverNotReachable") == "true")
				{
					tibco.admin.Main.alert(tibco.admin.Main.getServer().getDynamicProperty("@@lblError_37"),
							tibco.admin.Main.getServer().getDynamicProperty("@@serverNotReachableError_37"));
					return false;
				}

				// clear and put new string to the cache
				var selectMaskUddiPub_47 = this.block.getDescendantOfName("selectMaskUddiPub_47");
				tibco.admin.Main.getServer().getCache().clearById(this.block.getDescendantOfName("selectMaskUddiPub_47").getXMLId());

				//create businessList for uddi server info structure
				var xmlString = '<?xml version="1.0" encoding="UTF-8"?><data jsxid="jsxroot">';
				var serverRecordFirstChild = serverRecord.getFirstChild();
				var businessList = serverRecordFirstChild.getChildNodes();
				for(var j=0; j<businessList.size();j++) {
					var business = businessList.get(j);
					var businessName = business.getAttribute("businessName");

					//to replace the "&" with "&amp;"
					if(businessName.indexOf('&')>0){
						businessName = businessName.replace(/&/g,"&amp;");
					}
					var businessKey = business.getAttribute("businessKey");
					xmlString +='<record jsxid="'+ businessKey + '" jsximg="" jsxtext="'+businessName + '"/>';
				}

				xmlString += '</data>';
				selectMaskUddiPub_47.setXMLString(xmlString);
				return true;
			}

			plugin.prototype.mtx_onchange = function(selectedOptionText,strRECORDID ) {
				var uddiServerList = tibco.admin.Main.getServer().getCache().getDocument("uddiServerList");

				var jsxid = this.block.getDescendantOfName("mtxUddiServers").getSelectedIds()[0];
				var para = "/data/record[@jsxid='"+jsxid+"']";
				var serverRecordArray = uddiServerList.selectNodes(para);

				var btnUddiPub = document.getElementById(jsxid + "Publish");
				var selectTemp = tibco.admin.Main.getServer().getDynamicProperty("@@txtSelect_37");
				if(selectedOptionText != selectTemp ){
					btnUddiPub.style.color="#2f525d";
					btnUddiPub.style.backgroundColor="#e8e8f5";
				}
				return true;
			}

			// call publishService operation
			plugin.prototype.callpublishService = function() {
				var objService = tibco.admin.Main.loadService("tibco.admin",
						"tibco/admin/monitormanage/uddiPub/rules/publishService.xml",
						"publishService",
						"publishService");
				var matrix = this.block.getDescendantOfName("mtxUddiServers");
				var serverRecordArray = matrix.getSelectedNodes();

				var serverRecord = serverRecordArray.get(0);
				objService.EntityID = plugin.EntityID;
				objService.EntityName = plugin.EntityName;
				objService.uddiServerEntityID = serverRecord.getAttribute("uddiServerEntityID");
				objService.uddiServerEntityName = serverRecord.getAttribute("mcName");

				var mcStatus = serverRecord.getAttribute("mcStatus");
				if(mcStatus == "Unpublished"){
					objService.businessKey = serverRecord.getAttribute("mcBusiness");
					objService.businessName = matrix.getContentElement(matrix.getValue(),"mcBusiness").innerHTML;
				} else {
					objService.businessKey = serverRecord.getAttribute("publishedToBusinessKey");
					objService.businessName = serverRecord.getAttribute("mcBusiness");
				}

				//if didn't select a business,then return silent.
				var txtSelect_37 = tibco.admin.Main.getServer().getDynamicProperty("@@txtSelect_37");
				if(objService.businessName == txtSelect_37){
					return false;
				}

				if(tibco.admin.Main.getServer().getEnv("MODE")) {
					objService.setEndpointURL("/amxadministrator/services/monitorandmanage");
				}

				//subscribe and call
				objService.subscribe(jsx3.net.Service.ON_SUCCESS, this, this.onpublishServiceSuccess);
				objService.doCall();
			};

			plugin.prototype.onpublishServiceSuccess = function(objEvent) {
				this.callgetUDDIServerInfo();
			};

			plugin.getHandler = function(){
				return tibco.admin.Main.getServer().getJSXByName("blkUddiPub_37").handler;
			}
			plugin.getDlgHandler = function(){
				return tibco.admin.Main.getServer().getJSXByName("dlgUddiPub");
			}

			// Call unpublishService operation
			plugin.prototype.callunpublishService = function() {
				var objService = tibco.admin.Main.loadService("tibco.admin",
						"tibco/admin/monitormanage/uddiPub/rules/unPublishService.xml",
						"unpublishService",
						"unpublishService");

				if(tibco.admin.Main.getServer().getEnv("MODE")) {
					//objService.setEndpointURL(tibco.admin.util.ServiceHelper.getContainersEndpointURL());
					objService.setEndpointURL("/amxadministrator/services/monitorandmanage");
				}

				var matrix = this.block.getDescendantOfName("mtxUddiServers");
				var serverRecordArray = matrix.getSelectedNodes();

				var serverRecord = serverRecordArray.get(0);
				objService.EntityID = plugin.EntityID;
				objService.EntityName = plugin.EntityName;
				objService.uddiServerEntityID = serverRecord.getAttribute("uddiServerEntityID");
				objService.uddiServerEntityName = serverRecord.getAttribute("mcName");
				objService.subscribe(jsx3.net.Service.ON_SUCCESS, this, this.onunpublishServiceSuccess);
				objService.doCall();
			};

			plugin.prototype.onunpublishServiceSuccess = function(objEvent) {
				this.callgetUDDIServerInfo();
			};

			//get metadata for the uddipub.xml
			plugin.prototype.callgetMetadata = function() {
				var objService = tibco.admin.Main.loadService("tibco.admin",
						"tibco/admin/monitormanage/uddiPub/rules/getMetadataForService.xml",
						"getMetadataForService",
						"getMetadataForService");

				if(tibco.admin.Main.getServer().getEnv("MODE")) {
					//objService.setEndpointURL(tibco.admin.util.ServiceHelper.getContainersEndpointURL());
					objService.setEndpointURL("/amxadministrator/services/monitorandmanage");
				}

				objService.EntityID = plugin.EntityID;
				objService.EntityName = plugin.EntityName;
				objService.subscribe(jsx3.net.Service.ON_SUCCESS, this, this.ongetMetadataSuccess);
				objService.doCall();
			};

			plugin.prototype.ongetMetadataSuccess = function(objEvent) {
				this.block.getDescendantOfName("mtxMetadata").repaint();
			};

			//getInstancesOfService
			plugin.prototype.callgetInstancesOfService = function() {
				var objService = tibco.admin.Main.loadService("tibco.admin",
						"tibco/admin/monitormanage/uddiPub/rules/getInstancesOfService.xml",
						"getInstancesOfService",
						"getInstancesOfService");

				if(tibco.admin.Main.getServer().getEnv("MODE")) {
					//objService.setEndpointURL(tibco.admin.util.ServiceHelper.getContainersEndpointURL());
					objService.setEndpointURL("/amxadministrator/services/monitorandmanage");
				}

				objService.EntityID = plugin.EntityID;
				objService.EntityName = plugin.EntityName;
				objService.subscribe(jsx3.net.Service.ON_SUCCESS, this, this.ongetInstancesOfServiceSuccess);
				objService.doCall();
			};

			plugin.prototype.ongetInstancesOfServiceSuccess = function(objEvent) {
				this.block.getDescendantOfName("mtxInstanceOfService").repaint();
			};

			//get metadatas for the dialog
			plugin.prototype.callgetMetadataForService = function() {
				var objService = tibco.admin.Main.loadService("tibco.admin",
						"tibco/admin/monitormanage/uddiPub/rules/getMetadataForServiceDlg.xml",
						"getMetadataForService",
						"getMetadataForService");

				if(tibco.admin.Main.getServer().getEnv("MODE")) {
					//objService.setEndpointURL(tibco.admin.util.ServiceHelper.getContainersEndpointURL());
					objService.setEndpointURL("/amxadministrator/services/monitorandmanage");
				}
				objService.EntityID = plugin.EntityID;
				objService.EntityName = plugin.EntityName;
				objService.subscribe(jsx3.net.Service.ON_SUCCESS, this, this.ongetMetadataForServiceSuccess);
				objService.doCall();
			};

			plugin.prototype.ongetMetadataForServiceSuccess = function(objEvent) {
				this.uddiPubDlg.getDescendantOfName("mtxMetadataUddiPublish").repaint();
			};

			plugin.prototype.addMetadataRecord = function() {
				var cdfDoc = this.uddiPubDlg.getDescendantOfName("mtxMetadataUddiPublish").getXML();
				var newRecord = cdfDoc.createNode(jsx3.xml.Entity.TYPEELEMENT , "record");
				var nameColumn = this.uddiPubDlg.getDescendantOfName("txtMetadataName").getValue();

				if(nameColumn == undefined || nameColumn == '' || nameColumn.trim() == ''){
					tibco.admin.Main.alert(tibco.admin.Main.getServer().getDynamicProperty("@@lblError_37"),
							tibco.admin.Main.getServer().getDynamicProperty("@@inputMetadataNameError_37"));
					return false;
				}

				var valueColumn = this.uddiPubDlg.getDescendantOfName("txtMetadataValue").getValue();

				if(valueColumn == undefined || valueColumn == '' || valueColumn.trim() == ''){
					tibco.admin.Main.alert(tibco.admin.Main.getServer().getDynamicProperty("@@lblError_37"),
							tibco.admin.Main.getServer().getDynamicProperty("@@inputMetadataValueError_37"));
					return false;
				}

				newRecord.setAttribute("jsxid", jsx3.xml.CDF.getKey());
				newRecord.setAttribute("mcName", nameColumn);
				newRecord.setAttribute("mcValue", valueColumn );
				cdfDoc.appendChild(newRecord);
				this.uddiPubDlg.getDescendantOfName("mtxMetadataUddiPublish").repaint();
			};

			plugin.prototype.callsetMetadataForService = function() {
				var objService = tibco.admin.Main.loadService("tibco.admin",
						"tibco/admin/monitormanage/uddiPub/rules/setMetadataForService.xml",
						"setMetadataForService",
						"setMetadataForService");

				if(tibco.admin.Main.getServer().getEnv("MODE")) {
					objService.setEndpointURL("/amxadministrator/services/monitorandmanage");
				}
				objService.EntityID = plugin.EntityID;
				objService.EntityName = plugin.EntityName;
				objService.subscribe(jsx3.net.Service.ON_SUCCESS, this, this.onsetMetadataForServiceSuccess);
				objService.doCall();
			};

			plugin.prototype.onsetMetadataForServiceSuccess = function() {
				// remove the record in the dialog
				if(this.uddiPubDlg){
					this.uddiPubDlg.doClose();
					this.callgetMetadata();
					this.callgetUDDIServerInfo();
				}
			};

			plugin.prototype.removeMetadata = function() {
				this.callsetMetadataForService();
			};

			plugin.prototype.removeMetadataInDlg = function() {
				var mtxMetadata = this.uddiPubDlg.getDescendantOfName("mtxMetadataUddiPublish");
				var metadataId = mtxMetadata.getSelectedIds()[0];
				mtxMetadata.deleteRecord(metadataId,true);
			};

			plugin.prototype.callgetUDDIIDforService = function() {
				var objService = tibco.admin.Main.loadService("tibco.admin",
						"tibco/admin/monitormanage/uddiPub/rules/getUDDIIDforService.xml",
						"getUDDIIDforService",
						"getUDDIIDforService");

				if(tibco.admin.Main.getServer().getEnv("MODE")) {
					objService.setEndpointURL("/amxadministrator/services/monitorandmanage");
				}

				objService.EntityID = plugin.EntityID;
				objService.EntityName = plugin.EntityName;
				objService.subscribe(jsx3.net.Service.ON_SUCCESS, this, this.ongetUDDIIDforServiceSuccess );
				objService.doCall();
			};

			plugin.prototype.ongetUDDIIDforServiceSuccess = function(objEvent) {
				var CDF_UDDIID_37 = tibco.admin.Main.getServer().getCache().getDocument("CDF_UDDIID_37");
				var lblUDDIID_37 = CDF_UDDIID_37.getAttribute("lblUDDIID_37");
				tibco.admin.Main.getServer().getJSXByName("blkUddiPub_37").getDescendantOfName("lblUDDIID_37").setText(lblUDDIID_37,false);
			};
	});
}
